<template>
  <div class="controls">
    <div class="dropdown">
      <select name="one" class="dropdown-select">
        <option value="">Select…</option>
        <option value="1">机位 1</option>
        <option value="2">机位 2</option>
        <option value="3">机位 3</option>
        <option value="4">机位 4</option>
      </select>
    </div>
    <button class="submit-btn" @click="submitCameraSelection">提交</button>
  </div>

  <div class="container">
    <div class="video-container">
      <!-- Four img tags to display the video streams -->

<!--      <img class="video-stream" :src="videoSrc2" style="float: left;" />-->
      <!-- <img class="video-stream" :src="videoSrc3" /> -->
      <!-- <img class="video-stream" :src="videoSrc4" /> -->
<!--      <Video :rtsp="'rtsp://rtspstream:ac745a632b38e0c70c5d22751ce9b72c@zephyr.rtsp.stream/movie'" ref="videoSon"></Video>-->
      <div v-if="isVideoAvailable" class="video-wrapper">
        <img class="video-stream" :src="videoSrc2" style="float: left;" />
      </div>
      <div v-else class="video-placeholder">

<!--        <embed type="application/x-vlc-plugin" pluginspage="http://www.videolan.org"-->
<!--               width="640"-->
<!--               height="480"-->
<!--               id="vlc" />-->
        <div >
          <div class="loader"></div>
          <h2 style="color: white">Loading...</h2>
        </div>
      </div>
    </div>
  </div>

</template>

<script setup lang="ts">
import Video from '../video/video.vue'

import { ref, onMounted, onBeforeUnmount} from 'vue';
import { trackClick } from "@/api/video";
import { videoReg1, videoReg2, videoReg3, videoReg4 } from "@/api/types/multiplyVideo/recognizeVideo";
import { invade_close_click, monitor_close_click, register_close_click, track_close_click } from "@/api/close";
import { ElMessage } from 'element-plus'
import { globalVariable } from '../../store/modules/globalurl'


/**
 * 钩子函数实时调用
 */
onMounted(async () => {
  await close(); // Wait for close to finish
  checkVideoAvailability(); // Then check video availability
});

onBeforeUnmount(() => {
  close(); // Call close function when component is about to unmount
});
async function close() {
  try {
    const responses = await Promise.all([
      register_close_click(),
      invade_close_click(),
      track_close_click(),
      monitor_close_click()
    ]);
    // Handle success for all requests
    responses.forEach(response => {
      var res = response.data;
      if (res.respCode !== '000000') {
        ElMessage.error(res.respMsg);
      }
    });
  } catch (error) {
    console.error(error);
    ElMessage.error('关闭失败，请重试');
  }
}
//----------------------初始视频流-----------------------------
const videoSrc2 = ref(globalVariable+'/api/video_recognize/1');

const showCameraSelection = ref(false);
const selectedCamera = ref('');

function toggleCameraSelection() {
  showCameraSelection.value = !showCameraSelection.value;
  selectedCamera.value = '';
}

function submitCameraSelection() {

  let selectedCameraValue = selectedCamera.value; // 从 <select> 元素中获取选中的值

  if (selectedCameraValue === '1') {

    videoSrc2.value = globalVariable+'/api/video_recognize/1';
    // videoReg1();
    showCameraSelection.value = false;
    selectedCamera.value = '';
  }else if(selectedCameraValue === '2'){
    // videoReg2();
    videoSrc2.value = globalVariable+'/api/video_recognize/2';
    showCameraSelection.value = false;
    selectedCamera.value = '';
  }else if(selectedCameraValue === '3'){
    // videoReg3();
    videoSrc2.value = globalVariable+'/api/video_recognize/3';
    showCameraSelection.value = false;
    selectedCamera.value = '';
  }else if(selectedCameraValue === '4'){
    // videoReg4();
    videoSrc2.value = globalVariable+'/api/video_recognize/4';
    showCameraSelection.value = false;
    selectedCamera.value = '';
  }
}

const isVideoAvailable = ref(false);
// const isVideoAvailable = (src: string) => {
//   return !!src;
// }

const checkVideoAvailability = async () => {
  try {
    const response = await fetch(videoSrc2.value);
    if (response.status === 200) {
      isVideoAvailable.value = true;
    } else {
      console.error('获取视频1信息失败，状态码：', response.status);
    }
  } catch (error) {
    console.error('发生错误：', error);
  }
};
</script>

<style scoped>
.refresh-btn {
  float: right;
  padding: 10px 20px;
  background-color: #583C87;
  color: white;
  border: none;
  border-radius: 4px;
  cursor: pointer;
  font-size: 16px;
}

.camera-btn {
  padding: 10px 20px;
  background-color: #583C87;
  color: white;
  border: none;
  border-radius: 4px;
  cursor: pointer;
  font-size: 16px;
  margin-top: 10px;
}

.submit-btn {
  padding: 10px 20px;
  margin-top: 10px;
  margin-left: 10px;
  background-color: #583C87;
  color: white;
  border: none;
  border-radius: 4px;
  cursor: pointer;
  font-size: 16px;
}

.controls {
  display: flex;
  justify-content: center;
  align-items: center;
}

.video-container {
  display: grid;
  place-items: center;
  height: 100%;
}

.container {
  display: flex;
  justify-content: center;
  align-items: center;
}

.video-stream {
  width: 100%;
}

.video-placeholder {
  display: flex;
  justify-content: center;
  align-items: center;
  width: 750px;
  height: 500px;
  background-color: #3c5287;
  color: #333;
  font-size: 1.2em;
}

.loader {
  -webkit-animation: spin 1s linear infinite;
  animation: spin 1s linear infinite;
  border: 30px solid #ddd;
  border-bottom: 30px solid #64b5f6;
  border-radius: 50%;
  width: 100px;
  height: 100px;
}

@-webkit-keyframes spin {
  0% {
    -webkit-transform: rotate(0deg);
  }
  100% {
    -webkit-transform: rotate(-360deg);
  }
}

@keyframes spin {
  0% {
    transform: rotate(0deg);
  }
  100% {
    transform: rotate(-360deg);
  }
}

h2 {
  animation: typing 1s infinite;
  font-size: 30px;
  margin-top: 30px;
  overflow: hidden;
}

@keyframes typing {
  from {
    width: 70%;
  }
  to {
    width: 100%;
  }
}

.dropdown {
  display: inline-block;
  position: relative;
  overflow: hidden;
  height: 40px;
  width: 350px;
  margin-top: 10px;
  background: #f2f2f2;
  border: 1px solid;
  border-color: white #f7f7f7 whitesmoke;
  border-radius: 3px;
  background-image: -webkit-linear-gradient(top, transparent, rgba(0, 0, 0, 0.06));
  background-image: -moz-linear-gradient(top, transparent, rgba(0, 0, 0, 0.06));
  background-image: -o-linear-gradient(top, transparent, rgba(0, 0, 0, 0.06));
  background-image: linear-gradient(to bottom, transparent, rgba(0, 0, 0, 0.06));
  -webkit-box-shadow: 0 1px 1px rgba(0, 0, 0, 0.08);
  box-shadow: 0 1px 1px rgba(0, 0, 0, 0.08);
}

.dropdown:before, .dropdown:after {
  content: '';
  position: absolute;
  z-index: 2;
  top: 15px;
  right: 10px;
  width: 0;
  height: 0;
  border: 4px dashed;
  border-color: #888888 transparent;
  pointer-events: none;
}

.dropdown:before {
  border-bottom-style: solid;
  border-top: none;
}

.dropdown:after {
  margin-top: 7px;
  border-top-style: solid;
  border-bottom: none;
}

.dropdown-select {
  position: relative;
  width: 130%;
  margin: 0;
  padding: 6px 8px 6px 10px;
  height: 40px;
  line-height: 14px;
  font-size: 16px;
  color: #62717a;
  text-shadow: 0 1px white;
  background: #f2f2f2; /* Fallback for IE 8 */
  background: rgba(0, 0, 0, 0) !important; /* "transparent" doesn't work with Opera */
  border: 0;
  border-radius: 0;
  -webkit-appearance: none;
}

.dropdown-select:focus {
  z-index: 3;
  width: 100%;
  color: #394349;
  outline: 2px solid #49aff2;
  outline: 2px solid -webkit-focus-ring-color;
  outline-offset: -2px;
}

.dropdown-select > option {
  margin: 3px;
  padding: 6px 8px;
  text-shadow: none;
  background: #f2f2f2;
  border-radius: 3px;
  cursor: pointer;
}

/* Fix for IE 8 putting the arrows behind the select element. */

.lt-ie9 .dropdown {
  z-index: 1;
}

.lt-ie9 .dropdown-select {
  z-index: -1;
}

.lt-ie9 .dropdown-select:focus {
  z-index: 3;
}

/* Dirty fix for Firefox adding padding where it shouldn't. */

@-moz-document url-prefix() {
  .dropdown-select {
    padding-left: 6px;
  }
}


</style>